<!--
 * @Author: 敬华丰 2637844016@qq.com
 * @Date: 2023-04-27 14:07:01
 * @LastEditors: 敬华丰 2637844016@qq.com
 * @LastEditTime: 2023-05-05 09:43:29
 * @FilePath: \imedia_crm-branch_v5.0.1_0518\src\allocation\resourceManage\membership\components\c-membership-form.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <el-dialog :title="`${ruleForm.id ? '编辑' : '新增'}会员信息`" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <el-form-item label="会员等级" prop="grade">
                <c-number-input v-model="ruleForm.grade" placeholder="请输入会员等级"></c-number-input>
            </el-form-item>
            <el-form-item label="充值区间" prop="interval">
                <c-money-range @change="$refs.ruleForm.validateField('interval')" v-model="ruleForm.interval" />
            </el-form-item>
            <el-form-item label="优惠比例" prop="discountRatio">
                <c-number-input v-model="ruleForm.discountRatio" placeholder="请输入优惠比例"> <template slot="append"> % </template></c-number-input>
            </el-form-item>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    data () {
        return {
            dialogVisible: false,
            ruleForm: {
                interval: [],
            },
            loading: false,
            rules: {
                grade: [{ required: true, message: "请输入会员等级", trigger: "blur" }],
                interval: [
                    {
                        type: "array",
                        required: true,
                        message: "请输入充值区间",
                        trigger: "blur",
                    },
                ],
                discountRatio: [
                    { required: true, message: "请输入优惠比例", trigger: "blur" },
                ],
            },
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        data.rechargeMin = data.interval[0];
                        data.rechargeMax = data.interval[1];
                        this.$post({
                            url: `/MemberInfo/${data.id ? "update" : "insert"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {
            if (item && item.id) {
                this.ruleForm = {
                    ...item,
                    interval: [item.rechargeMin, item.rechargeMax],
                };
            }
            this.dialogVisible = true;
        },
        handleClose () {
            this.ruleForm = {};
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
